let token = localStorage.token;

let cartName = localStorage.cartName;
let username = localStorage.username;
let valueNum = localStorage.valueNum;
valueNum = valueNum.split(',');
let informaEl = document.querySelector('.informa');
let allEl = document.querySelector('.all');
let nrEl = document.querySelector('.nr');
let allPrice = 0;
let nowNum = 0
console.log(username);
async function isFav() {
    try {
        let address = await axios({
            method: 'post',
            url: 'http://localhost:9527/api/user/getAdd',
            data: {
                "username": username
            },
            headers: {
                'Authorization': 'Bearer ' + token
            }
        })
        cartName = cartName.split(',')
        await cartName.forEach((e) => {
            let thisGoods = axios({
                method: 'post',
                url: 'http://localhost:9527/api/data/getGoods',
                data: {
                    "cartName": e
                },
            }).then((res) => {
                let goods = res.data.data.goods[0]
                let cartImg = goods.cartImg
                let cartDes = goods.cartDes
                let cartSpc = goods.cartSpc
                let cartPrice = goods.cartPrice
                let cartType = goods.cartType
                let _id = goods._id
                let divEl = document.createElement('div')
                value = valueNum[nowNum]
                divEl.innerHTML = `
            <img src="${cartImg}" alt="">
            <div class="box">
                <div class="biaot">${cartDes}
                </div>
                <span>￥${cartPrice}</span>
                <input type="text" value="x${value}">
            </div>
             `
                nrEl.append(divEl)

                allPrice += cartPrice * value
                allEl.innerHTML = `
             <div><span>商品价格</span><span class="all_price">￥${allPrice}</span></div>
             <div><span>运费</span><span>￥0.00</span></div>
             <div><span>税费</span><span>￥0.00</span></div>
            `
                nowNum++

            })

        })
        let defAdd = address.data.data
        Object.keys(defAdd).forEach((e) => {
            if (defAdd[e].isDef) {
                let phoneNum = defAdd[e].phoneNum
                let realName = defAdd[e].realName
                let userAdd = defAdd[e].userAdd
                informaEl.innerHTML = `
            <div>&nbsp&nbsp&nbsp收货人：<span>${realName}</span></div>
            <div>电话：<span>${phoneNum}</span></div>
            <div>&nbsp&nbsp&nbsp地址：<span>${userAdd}</span></div>
            <div class="changeAdd">其他地址</div>
            `
            }
        })

        let changeAddEl = document.querySelector('.changeAdd')


        let back = document.querySelector('.back')
        document.onclick = function (el) {
            el = el.target
            if (el === back) {
                history.back()
            }
            if (el.closest('.changeAdd')) {
                location.href = "../YJJ/Address.html"
            }
            if (el.closest('.subBtn')) {
                if(!defAdd[0]) return alert('请添加地址')
                localStorage.setItem("allPrice", allPrice)
                location.href = "../WXL/fukfs.html"
            }
        }
        let usernameEl = document.querySelector('.name')
        let iphto = document.querySelector('.iphto')
        let addressEl = document.querySelector('.address')
        class Abcd {
            constructor(arr) {
                this.arr = arr
                this.name = this.arr.name
                this.telphon = this.arr.telphon
                this.emaix = this.arr.emaix
                this.nameone()
                this.telphonone()
                this.emaixone()
            }
            nameone() {
                // 名字
                usernameEl.innerHTML = `<option>${this.name}</option>`
            }
            telphonone() {
                // 电话
                let m = ''
                this.telphon.forEach(item => {
                    m += `<option>${item}</option>`
                });
                iphto.innerHTML = m
            }
            emaixone() {
                // 地址
                let n = ''
                this.emaix.forEach(item => {
                    n += `<option>${item}</option>`
                });
                addressEl.innerHTML = n
            }
        }

        arr = {
            name: 'sdada',
            telphon: [123456789, 32141312],
            emaix: ['daskudhakhdkajshdas', 'diausdhaiushdauisdhu', 'sadasuidiasdia']
        }
        // let board = new Abcd(arr)


    } catch (error) {
        if (error.response.status == 401) {
            // 鉴权失败
            alert('用户未登录，请登录后进行访问')
            location.href = "../../html/LXW/login.html"
        }
    }
}
isFav()